import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 解决导航栏或者底部导航tabBar中的vue-router在3.0版本以上频繁点击菜单报错的问题。
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

const routes = [{
        path: '/',
        redirect: "/login"
    },
    {
        path: '/login',
        name: 'Login',
        meta: { name: '登录' },
        component: () =>
            import ('../components/Login.vue'),
        beforeEnter: (to, from, next) => {
            if (Boolean(localStorage.getItem("token"))) {
                next({
                    path: '/front'
                })
            }
            next()
        }
    },
    {
        path: '/front',
        name: 'Front',
        component: () =>
            import ('../views/Front.vue'),
        children: [{
                path: 'home',
                name: 'FHome',
                meta: { name: '系统首页' },
                component: () =>
                    import ('../views/front/Home')
            },
            {
                path: 'person',
                name: 'Person',
                meta: { name: '个人信息' },
                component: () =>
                    import ('../views/front/Person')
            },
            {
                path: 'blogDetail',
                name: 'BlogDetail',
                meta: { name: '博客详情' },
                component: () =>
                    import ('../views/front/BlogDetail')
            },
            { path: 'search', name: 'Search', meta: { name: '博客搜索' }, component: () =>
                    import ('../views/front/Search') },
            {
                path: 'newBlog',
                name: 'NewBlog',
                meta: { name: '新增博客' },
                component: () =>
                    import ('../views/front/NewBlog')
            },
        ]
    },
    {
        path: '/register',
        name: 'Register',
        meta: { name: '注册' },
        component: () =>
            import ('../views/Register.vue')
    },
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

// 注：不需要前台的项目，可以注释掉该路由守卫
// 路由守卫
// router.beforeEach((to, from, next) => {
//     let user = JSON.parse(localStorage.getItem("xm-user") || '{}');
//     if (to.path === '/') {
//         if (user.role) {
//             if (user.role === 'USER') {
//                 next('/front/home')
//             } else {
//                 next('/home')
//             }
//         } else {
//             next('/login')
//         }
//     } else {
//         next()
//     }
// })

export default router